package com.calvin.tingapp.actions;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.calvin.android.EventDispatch;
import com.calvin.android.annotation.ViewInject;
import com.calvin.android.json.JSONArray;
import com.calvin.android.json.JSONObject;
import com.calvin.android.notification.NotificationCenter;
import com.calvin.android.utils.CUtils;
import com.calvin.tingapp.MainActivity;
import com.calvin.tingapp.R;
import com.calvin.tingapp.adapter.BookListViewAdapter;
import com.calvin.tingapp.config.NTConfig;
import com.calvin.tingapp.service.SerializationCenter;

import java.util.Iterator;

public class BookListAction {
    public static final int T_LOCAL = 0;
    public static final int T_LIKE = 1;
    public static final int T_RECORD = 2;

    int mType = 0;

    @ViewInject(id = R.id.listView)
    ListView mListView;

    JSONArray mBookList = new JSONArray();


    public BookListAction(int type) {
        mType = type;
    }

    @ViewInject(isTag = true, tag = "nav_title", listen = EventDispatch.CListen.INIT)
    public void onInit(View view) {

        int titleId = 0;
        switch (mType) {
            case T_LOCAL:
                titleId = R.string.s_local_book;
                break;
            case T_LIKE:
                titleId = R.string.s_like_book;
                break;
            case T_RECORD:
                titleId = R.string.s_record_book;
                break;
        }
        CUtils.cast(view, TextView.class).setText(titleId);

        buildBookList();
        mListView.setAdapter(new BookListViewAdapter(MainActivity.sharedCurrent(), mBookList));
    }

    public void buildBookList() {
        switch (mType) {
            case T_LOCAL:
                mBookList = SerializationCenter.shared().getDwonloadBookList();
                break;
            case T_RECORD:
                mBookList = SerializationCenter.shared().getRecordBookList();
                break;
        }
    }

    @ViewInject(isTag = true, tag = "nav_left_button")
    public void onNavLeftClick(View view) {
        NotificationCenter.shared().postNotice(NTConfig.NT_NAV_BACK);
    }

    @ViewInject(id = R.id.listView, listen = EventDispatch.CListen.ITEMCLICK)
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        NotificationCenter.shared().postNotice(NTConfig.NT_NAV_NEXT, R.layout.action_detail, new DetailAction(mBookList.getJSONObject(position)));
    }


}
